
import 'package:file_picker/file_picker.dart';
import 'package:file_transfer/model/simple_model.dart';
import 'package:file_transfer/widget/app_bar_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ConfigPage extends StatelessWidget {
  const ConfigPage({super.key});

  _getDir(BuildContext context) async{
    final simpleModel = context.read<SimpleModel>();
    final res = await FilePicker.platform.getDirectoryPath();
    if(res == null){
      return;
    }
    debugPrint("目录：$res");
    simpleModel.receiveDirectoryPath = res;
  }

  _item(String title, {required VoidCallback onTap}){
    final border = RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
    );
    return Container(
      margin: const EdgeInsets.only(top: 5),
      child: Material(
        color: Colors.white,
        shape: border,
        child: InkWell(
          customBorder: border,
          onTap: onTap,
          child: Container(
            height: 50,
            /*decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.0)
            ),*/
            padding: const EdgeInsets.only(top: 4,bottom: 4,left: 8,right: 8),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title,style: const TextStyle(
                    color: Colors.black,
                    fontSize: 18
                )),
                const Icon(Icons.chevron_right_sharp,color: Color(0xffBFBFBF))
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: pageAppBar("个性设置", context),
      body: Container(
        color: Color(0xffF4F4F4),
        child: Container(
          padding: const EdgeInsets.all(5.0),
          margin: const EdgeInsets.all(5.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text("文件",style: TextStyle(
                  color: Colors.blueAccent,
                  fontSize: 16,
                  fontWeight: FontWeight.bold
              ),),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Selector<SimpleModel,String>(builder: (BuildContext context, String value, Widget? child) {
                        return Text(value);
                      }, selector: (_ , SimpleModel simpleModel)=>simpleModel.receiveDirectoryPath),
                      _item("修改文件保存路径",onTap: ()=> _getDir(context))
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }


  Widget build2(BuildContext context) {
    return Scaffold(
      appBar: pageAppBar("个性设置", context),
      body: Container(
        color: Colors.grey[300],
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(5.0),
          margin: const EdgeInsets.all(5.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text("文件",style: TextStyle(
                  color: Colors.blueAccent,
                  fontSize: 16,
                  fontWeight: FontWeight.bold
              ),),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Selector<SimpleModel,String>(builder: (BuildContext context, String value, Widget? child) {
                        return Text(value);
                      }, selector: (_ , SimpleModel simpleModel)=>simpleModel.receiveDirectoryPath),
                      _item("文件保存路径",onTap: ()=> _getDir(context))
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
